package com.yc.navy.dayfresh.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.navy.dayfresh.bean.ProductInfo;
import com.yc.navy.dayfresh.enums.ResultEnum;
import com.yc.navy.dayfresh.service.IProductInfoService;
import com.yc.navy.dayfresh.util.ConstantInfo;
import com.yc.navy.dayfresh.vo.ResultVO;

@RestController
@RequestMapping("/product")
public class ProductInfoController {
	@Autowired
	private IProductInfoService productInfoSerivce;
	
	@PostMapping("/upload")
	public Map<String, Object> upload(MultipartFile upload){
		String savepath = "";
		File dest = null;
		Map<String,Object> map = new HashMap<String,Object> ();
		if(upload != null && upload.getSize() > 0) {
			try {
				savepath = ConstantInfo.UPLOADPATH + "/" + System.currentTimeMillis() + "_" + upload.getOriginalFilename();
				dest = new File(ConstantInfo.basepath,savepath);
				upload.transferTo(dest);
				
				//TODO 添加图片水印
				map.put("filename",upload.getOriginalFilename());
				map.put("uploaded",1);
				map.put("url", "../../../" + savepath);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} 
		return map;
	}
	
	@PostMapping("/add")
	public ResultVO add(ProductInfo pf, MultipartFile[] photos) {
		String savepath = "";
		File dest = null;
		if(photos != null && photos.length >0 && photos[0].getSize() >0) {
			String picStr = "";
			try {
				for(MultipartFile fl: photos) {
					savepath = ConstantInfo.UPLOADPATH + "/" + System.currentTimeMillis() + "_" + fl.getOriginalFilename();
					dest = new File(ConstantInfo.basepath,savepath);
					fl.transferTo(dest);
					if("".equals(picStr)) {
						picStr = savepath;
					}else {
						picStr +=";" +savepath;
					}
				}
				pf.setPics(picStr);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		int result = productInfoSerivce.add(pf);
		if(result == -1) {
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}
		
		if(result >0) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROR);
	}
	
	@GetMapping("/findIndex")
	public Map<String, Object> findIndex(){
		return productInfoSerivce.findIndex();
	}
	
	@PostMapping("/findByPageFirst")
	public ResultVO findByPageFirst(@RequestParam Map<String, Object> map) {
		Map<String, Object> result = productInfoSerivce.findFirstByPage(map);
		if(result == null || result.isEmpty()) {
			return new ResultVO(ResultEnum.DATA_NULL);
		}
		return new ResultVO(ResultEnum.SUCCESS,result);
	}
	
	@PostMapping("/findByPage")
	public ResultVO findByPage(@RequestParam Map<String,Object> map) {
		List<ProductInfo> list = productInfoSerivce.findByPage(map);
		if(list == null || list.isEmpty()) {
			return new ResultVO(ResultEnum.DATA_NULL);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	
	@PostMapping("/findByPno")
	public ResultVO findByPno(String pno) {
		ProductInfo pf = productInfoSerivce.findByPno(pno);
		if(pf == null) {
			return new ResultVO(ResultEnum.DATA_NULL);
		}
		return new ResultVO(ResultEnum.SUCCESS,pf);
	}
}
